package com.example.android.utils;

import android.media.MediaPlayer;
import android.text.TextUtils;

/**
 * 语音播放
 *
 * @author zhouguanghong
 * @version 1.0.0
 * create at 2018/7/13
 */

public class AudioPlayer implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

    private final float DEFAULT_VOLUME = 1.0f;
    /**
     * 播放组件
     */
    private MediaPlayer mPlayer;
    /**
     * 播放状态接口
     */
    private AudioPlayStatusListener audioPlayStatusListener;
    /**
     * 当前状态
     */
    private AudioPlayStatus nowStatus = AudioPlayStatus.STOPED;
    /**
     * 当前播放音频
     */
    private String mAudioPath;

    /**
     * 是否是循环播放
     */
    private boolean looping;

    /**
     * 播放器的音量
     */
    private float mVolume = DEFAULT_VOLUME;

    public void setAudioPlayStatusListener(AudioPlayStatusListener audioPlayStatusListener) {
        this.audioPlayStatusListener = audioPlayStatusListener;
    }

    /**
     * 开始播放
     *
     * @param path 使用tag来区分是否是同一个音频，同一个出发点
     */
    public void start(String path) {
        if (TextUtils.isEmpty(path)) {
            //路径正确性判断
            setError(AudioPlayError.PATH_ERROR, path);
            return;
        }

        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
        }

        if (mAudioPath != null
                && mAudioPath.equals(path)) {
            //如果播放音频就是当前的音频
            if (nowStatus == AudioPlayStatus.PAUSE) {
                //继续播放
                mPlayer.seekTo(mPlayer.getCurrentPosition());
                mPlayer.start();

                setStatus(AudioPlayStatus.PLAYING, path);
            } else if (nowStatus == AudioPlayStatus.STOPED) {
                //已停止的音频，开始播放
                mPlayer.reset();
                play(mAudioPath);
            }
        } else {
            //不是同一个音频的情况
            if (nowStatus == AudioPlayStatus.PLAYING
                    || nowStatus == AudioPlayStatus.PAUSE) {
                //如果现在是播放、暂停，停止当前播放，再播放新的音频
                mPlayer.stop();
                mPlayer.reset();
            } else {
                //其他情况直接播放
                mPlayer.reset();
            }
            if (mAudioPath != null) {
                setStatus(AudioPlayStatus.STOPED, mAudioPath);
            }
            play(path);
        }
    }

    /**
     * 播放过程中的错误
     *
     * @param error
     */
    public void setError(AudioPlayError error, String audio) {
        if (audioPlayStatusListener != null) {
            audioPlayStatusListener.onError(error, audio);
        }
    }

    /**
     * 播放状态
     *
     * @param status
     */
    private void setStatus(AudioPlayStatus status, String audio) {
        nowStatus = status;

        if (audioPlayStatusListener != null) {
            audioPlayStatusListener.audioPlayStatus(status, audio);
        }
    }

    /**
     * 播放
     *
     * @param audio
     */
    private void play(final String audio) {
        try {
            mPlayer.setVolume(mVolume, mVolume);
            mPlayer.setDataSource(audio);
            mPlayer.setLooping(looping);
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mPlayer.seekTo(0);
                    mPlayer.start();

                    setStatus(AudioPlayStatus.PLAYING, audio);
                }
            });
            mPlayer.prepareAsync();

            mAudioPath = audio;
        } catch (Exception e) {
            e.printStackTrace();
            mPlayer = null;
        }
    }

    /**
     * 播放进度
     */
    public float playProgress() {
        if (mPlayer != null) {
            return ((float) mPlayer.getCurrentPosition()) / mPlayer.getDuration();
        }
        return 0.0f;
    }

    public int getCurrentPostion() {
        if (mPlayer != null) {
            return mPlayer.getCurrentPosition();
        }

        return 0;
    }

    public int getDuration() {
        if (mPlayer != null) {
            return mPlayer.getDuration();
        }

        return 0;
    }

    /**
     * 暂停播放
     */
    public void pause() {
        if (mPlayer != null) {
            mPlayer.pause();
        }

        if (mAudioPath != null) {
            setStatus(AudioPlayStatus.PAUSE, mAudioPath);
        }
    }

    /**
     * 播放完成
     *
     * @param mp
     */
    @Override
    public void onCompletion(MediaPlayer mp) {
        //判断是否还在播放中：当播放出现异常的时候也会调用这个方法
        if (!mPlayer.isPlaying()) {
            stop();
        }
    }

    /**
     * 停止播放，并且释放资源
     */
    public void stop() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }

        if (mAudioPath != null) {
            setStatus(AudioPlayStatus.STOPED, mAudioPath);
        }
         audioPlayStatusListener = null;
    }

    /**
     * 是否在播放中
     *
     * @return
     */
    public boolean isPlaying() {
        return mPlayer != null && mPlayer.isPlaying();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        stop();
        setError(AudioPlayError.ERROR, mAudioPath);
        return false;
    }

    public boolean isLooping() {
        return looping;
    }

    public void setLooping(boolean looping) {
        this.looping = looping;
    }

    public float getVolume() {
        return mVolume;
    }

    public void setVolume(float volume) {
        mVolume = volume;
        if (nowStatus == AudioPlayStatus.PLAYING || nowStatus == AudioPlayStatus.PAUSE) {
            mPlayer.setVolume(mVolume, mVolume);
            System.out.println("volume: " + volume);
        }
    }
}
